home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9416 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.9 KB  |  135 lines

  1. Path: surfnet.nl!sun4nl!xs4all!falstaff
  2. From: falstaff@xs4all.nl (Falstaff)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: PLEASE Help! :(
  5. Date: 10 Mar 1996 15:42:40 GMT
  6. Organization: XS4ALL, networking for the masses
  7. Message-ID: <4hut9g$p15@news.xs4all.nl>
  8. References: <4hsqk2$4d7@deadbird.db.erau.edu>
  9. NNTP-Posting-Host: xs1.xs4all.nl
  10. X-Newsreader: NN version 6.5.0 #666 (NOV)
  11.  
  12. kochank@news.db.erau.edu (Konrad Kochan) writes:
  13.  
  14. >Howdy.. I have a program to write, but I'm clueless... Now I could
  15. >give you a 100 excuses and blame the instructor (and I should cause
  16. >all of it would be true.. :)
  17.  
  18. >Anyways, I'm clueless.. can someone, a good samaritan (or someone just
  19. >bored..) help me out!
  20.  
  21. >Here is the program statement..
  22.  
  23. >Write a C program to sort, using any sorting method, a text file
  24. >named RECORDS.DAT, of unknown length, of records organized as follows:
  25.  
  26. >Field 1.  Contains an integer.
  27. >Field 2.  Contains a string of exactly 20 characters.
  28. >Field 3.  Contains a float followed by a newline character.
  29.  
  30. >Example of file organization:
  31.  
  32. >234 Numa Chaikin        33000.0
  33. > 75 advertisements      55.55
  34. >901 Shepard             15255.5
  35. >  3 Brook               120050.0
  36. >888 non-descriptive     0.0
  37.  
  38. >Sorting has to be done lexicographically, that is, by the second field
  39. >using alphabetical order.  This means that:
  40.  
  41. >1. Upper-case and lower-case letters should be treated identically.
  42. >   For example, the right order for a part of the list above is:
  43. >   888 non-descriptive 0.0
  44. >   234 Numa Chaikin 33000.0
  45.  
  46. >2. The only punctuation sign used in strings is a dash '-', which
  47. >   should be ignored in sorting (but not removed from words).
  48. >And here is what I got so far.. which DOESN'T work (right or otherwise, I
  49. >can't even get the program to scan the right fields from the file.. :(
  50.  
  51. >#include <stdio.h>
  52. >#include <stdlib.h>
  53. >#include <string.h>
  54.  
  55. >/* Global Variable Definition */
  56.  
  57. >int IntArr[512][5];
  58. >char WordsArr1[512][10];
  59. >char WordsArr2[512][10];
  60. >float FloatArr[512][5];
  61. >char inbuff[80];
  62. >int index=0;
  63.  
  64. >/* Function declaration */
  65.  
  66. >void Welcome(void);
  67. >void Goodbye(void);
  68.  
  69. >main()
  70. >{
  71. >/* Next function opens a file, if the file is missing, it prints
  72. > an error message */
  73. >int i;
  74. > FILE * fin, * fout;
  75. >    if (!(fin = fopen("RECORDS.DAT", "r")))
  76. >     {
  77. >    printf("Something is wrong: Probably file missing.\n");
  78. >    exit(1);   /* Program exits here if file is missing. */
  79. >            }
  80. >    printf("\nInput File = RECORDS.DAT\n");
  81. >/* This function prints a Welcome message */
  82.  
  83. >/* Welcome(); */
  84.  
  85. >/* This function scans in the array */
  86.  
  87. >    while (fgets(inbuff,80,fin) !=NULL)
  88. >{
  89. >sscanf(inbuff,"%d %s %s %4.2f", &IntArr[index],&WordsArr1[index],&WordsArr2[index],&FloatArr[index]);
  90. >index++;
  91. >}
  92.  
  93. >/* Opens the output file */
  94.  
  95. >if (!(fout = fopen("test", "w")))
  96. >     {
  97. >    printf("Something is wrong: File might be write protected.\n");
  98. >    exit(1);   /* Program exits here if file can't be opened. */
  99. >            }
  100.  
  101. >    for (i=0; i<index; i++)
  102. >    fprintf(fout, "%d %s %s %4.2f\n",  IntArr[i], WordsArr1[i],WordsArr2[i], 
  103. >     FloatArr[i]);
  104. >        
  105. >/* Prints the sorted array to the screen */
  106. >    
  107. >    for (i=0; i<index; i++)
  108. >    printf("Original array: %d %s %s %4.2f \n", IntArr[i],WordsArr1[i],
  109. >WordsArr2[i],FloatArr[i]);
  110. >fclose(fin);
  111. >fclose(fout);
  112. >}
  113. >If you can either give me hints, rewrite the above, or write a totaly
  114. >new program I would appreciate it.. I know its a lot to ask for (to do some-
  115. >one elses homework pretty much..) but I'm really clueless..
  116.  
  117. You must be if you think we're going to solve your homework for you.
  118. But, I'm feeling helpful today so I'll write your program.
  119. Here it is:
  120.  
  121. int main()
  122. {  
  123.    return system("sort -f +2 -3 -o RECORDS.DAT RECORDS.DAT");
  124. }
  125.  
  126. Payment before the end of the month by crossed check or postal order
  127. for US$100 to my home address please.
  128.  
  129. Frank
  130. --
  131. The famous GIICM now on line:  http://www.xs4all.nl/~falstaff/GIICM.html
  132. ------------------------------------------------------------------------
  133. Frank A. Vorstenbosch        +31-(70)-355 5241        falstaff@xs4all.nl
  134.